package org.noka.http;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * <p>@Title https</p>
 * <p>@Description </p>
 * <p>@Version 1.0.0</p>
 * <p>@author rebin</p>
 * <p>@date 2017年7月20日</p>
 * <p>xiefangjian@163.com</p>
 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
 */
public class HttpTools {
	private RequestConfig requestConfig;//请求配置
	private String charset = "UTF-8";
	private String defaulturl = null;//默认服务器地址
	private Map<String,String> header = null;//请求头参数
	//====================================文件上传与下载====================================================================================
	/**
	 * *******************************************************
	 * <p>@Description 下载文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:43:21</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public InputStream downloadFile(String url){
		return DoFile(url,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 下载文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:44:31</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public InputStream downloadFile(String url,Map<String,String> headers){
		return DoFile(url,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 下载文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月20日 上午11:18:54</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private InputStream DoFile(String url,Map<String,String> headers){
		PoolingHttpClientConnectionManager secureConnectionManager = null;
		InputStream instream = null;
		url=durl(url);
		try {
			secureConnectionManager =createManager(url);
			HttpClient httpclient = createHttpClient(secureConnectionManager);
			HttpGet httpget = new HttpGet(url);
			getHeaders(httpget,headers);
			httpget.setConfig(requestConfig);
			HttpResponse httpResponse = httpclient.execute(httpget);
			HttpEntity entity = httpResponse.getEntity();
			if (entity != null) {
				instream = entity.getContent();
			}
		} catch (Exception se) {
			se.printStackTrace();
		}finally{
			if(null!=secureConnectionManager){
				secureConnectionManager.shutdown();
				secureConnectionManager.close();
			}
		}
		return instream;
    }
	/**
	 * *******************************************************
	 * <p>@Description 下载并保存文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:46:48</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public Boolean downloadFileSaveFile(File file, String url){
		return DoBooleanFile(file,url,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 下载并保存文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:47:00</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public Boolean downloadFileSaveFile(File file, String url,Map<String,String> headers){
		return DoBooleanFile(file,url,headers);
	}
	/**
	 * *******************************************************
	 * <p>@Description 下载文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月20日 上午11:21:34</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private Boolean DoBooleanFile(File file, String url,Map<String,String> headers){
		PoolingHttpClientConnectionManager secureConnectionManager = null;
		try {
			url=durl(url);
			secureConnectionManager =createManager(url);
			HttpClient httpclient = createHttpClient(secureConnectionManager);
			HttpGet httpget = new HttpGet(url);
			getHeaders(httpget,headers);
			httpget.setConfig(requestConfig);
			HttpResponse httpResponse = httpclient.execute(httpget);
			HttpEntity entity = httpResponse.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				OutputStream out = new FileOutputStream(file);
				byte tmp[] = new byte[1024];
				int len;
				while ((len = instream.read(tmp, 0, 1024)) != -1) {
					out.write(tmp, 0, len);
				}
				out.close();
				instream.close();
			}
			return true;
		} catch (Exception se) {
		}finally{
			if(null!=secureConnectionManager){
				secureConnectionManager.shutdown();
				secureConnectionManager.close();
			}
		}
		return false;
	}
	/**
	 * *******************************************************
	 * <p>@Description 上传本地文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:48:29</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult uploadFile(File f, String url){
		return UpFile(f, url,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 上传本地文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:48:42</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult uploadFile(File f, String url,Map<String,String> headers){
		return UpFile(f, url,headers);
	}
	/**
	 * *******************************************************
	 * <p>@Description 上传文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月20日 下午3:01:10</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private HttpResult UpFile(File f, String url,Map<String,String> headers) {
		PoolingHttpClientConnectionManager secureConnectionManager = null;
		try {
			url=durl(url);
			secureConnectionManager =createManager(url);
			HttpClient httpclient = createHttpClient(secureConnectionManager);
			HttpPost httpost = new HttpPost(url);
			httpost.setConfig(requestConfig);
			FileBody file = new FileBody(f);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
			builder.addPart("media", file);
			httpost.setEntity(builder.build());
			postHeaders(httpost,headers);
			HttpResponse httpResponse = httpclient.execute(httpost);
			return new HttpResult(httpResponse.getStatusLine().getStatusCode(),EntityUtils.toString(httpResponse.getEntity()));
		} catch (Exception se) {
			return new HttpResult(900, se.getMessage());
		}finally{
			if(null!=secureConnectionManager){
				secureConnectionManager.shutdown();
				secureConnectionManager.close();
			}
		}
	}
	/**
	 * *******************************************************
	 * <p>@Description 上传文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:50:02</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult uploadInputStream(InputStream in, String filename, String url){
		return UpInputStream(in,filename,url,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 上传文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:50:12</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult uploadInputStream(InputStream in, String filename, String url,Map<String,String> headers){
		return UpInputStream(in,filename,url,headers);
	}
	
	/**
	 * *******************************************************
	 * <p>@Description 上传文件</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月20日 下午3:01:23</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private HttpResult UpInputStream(InputStream in, String filename, String url,Map<String,String> headers) {
		PoolingHttpClientConnectionManager secureConnectionManager = null;
		try {
			url=durl(url);
			secureConnectionManager =createManager(url);
			HttpClient httpclient = createHttpClient(secureConnectionManager);
			HttpPost httpost = new HttpPost(url);
			HFileBody file = new HFileBody(in, filename);
			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
			builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
			builder.addPart("media", file);
			httpost.setEntity(builder.build());
			postHeaders(httpost,headers);
			HttpResponse httpResponse = httpclient.execute(httpost);
			return new HttpResult(httpResponse.getStatusLine().getStatusCode(),EntityUtils.toString(httpResponse.getEntity()));
		} catch (Exception se) {
			return new HttpResult(900, se.getMessage());
		}finally{
			if(null!=secureConnectionManager){
				secureConnectionManager.shutdown();
				secureConnectionManager.close();
			}
		}
	}
	//=====================post==========================================================================================
	/**
	 * *******************************************************
	 * <p>@Description post json内容</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:29:54</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
    public HttpResult post(String url,String json){
    	return httppost(url,json,null,null);
    }
    /**
     * *******************************************************
     * <p>@Description post json和参数</p>
     * <p>@Version 1.0.0</p>
     * <p>@author rebinrebin</p>
     * <p>@time 2017年9月8日 上午9:30:08</p>
     * <p>xiefangjian@163.com</p>
     * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
     * *****************************************************
     */
    public HttpResult post(String url,String json,Map<String,String> headers){
    	return httppost(url,json,null,headers);
    }
    /**
     * *******************************************************
     * <p>@Description post参数</p>
     * <p>@Version 1.0.0</p>
     * <p>@author rebinrebin</p>
     * <p>@time 2017年9月8日 上午9:30:23</p>
     * <p>xiefangjian@163.com</p>
     * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
     * *****************************************************
     */
    public HttpResult post(String url,Map<String,String> pars){
    	return httppost(url,null,pars,null);
    }
    /**
     * *******************************************************
     * <p>@Description post参数和heads</p>
     * <p>@Version 1.0.0</p>
     * <p>@author rebinrebin</p>
     * <p>@time 2017年9月8日 上午9:30:35</p>
     * <p>xiefangjian@163.com</p>
     * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
     * *****************************************************
     */
    public HttpResult post(String url,Map<String,String> pars,Map<String,String> headers){
    	return httppost(url,null,pars,headers);
    }
    /**
     * *******************************************************
     * <p>@Description post请求</p>
     * <p>@Version 1.0.0</p>
     * <p>@author rebinrebin</p>
     * <p>@time 2017年9月8日 上午9:30:58</p>
     * <p>xiefangjian@163.com</p>
     * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
     * *****************************************************
     */
    public HttpResult post(String url){
    	return httppost(url,null,null,null);
    }
    /**
     * *******************************************************
     * <p>@Description post josn和参数</p>
     * <p>@Version 1.0.0</p>
     * <p>@author rebinrebin</p>
     * <p>@time 2017年9月8日 上午9:51:08</p>
     * <p>xiefangjian@163.com</p>
     * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
     * *****************************************************
     */
    public HttpResult post(String url,String json,Map<String,String> pars,Map<String,String> headers){
    	return httppost(url,json,pars,headers);
    }
	/**
	 * *******************************************************
	 * <p>@Description 请iu参数</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月19日 上午11:01:02</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private HttpResult httppost(String url,String json,Map<String,String> pars,Map<String,String> headers){
		 url=durl(url);
		 HttpPost httpost = null;
		 PoolingHttpClientConnectionManager secureConnectionManager = null;
		 StringBuffer urlpars = new StringBuffer();//参数
		 try{
			 	//------------------当put json有内容时，需要将参数以URL的方式追加到URL后面-----------------------------------------------------------------------------
			    if(json != null && json.trim().length() > 0 && pars != null && !pars.isEmpty()){
		            if(url.indexOf("?") == -1){
		                url+="?";//补全问号
		            }
		            for (Map.Entry<String, String> entry : pars.entrySet()) {  
		            	urlpars.append("&"+entry.getKey()+"="+URLEncoder.encode(entry.getValue(),charset));//追加参数
		            }
		        }
			    if(urlpars.toString().length()>3){
			 		url+=urlpars.toString().substring(1);
			 	}
			    //----------------初始化请求参数-------------------------------------------------------------------------------------------------------------
			    httpost = new HttpPost(url);// 创建httpPost    
				httpost.setConfig(requestConfig);
			    //----------------当put没有内容，直接以POST方式传递参数--------------------------------------------------------------------------------------------
		        if(json == null && pars != null && !pars.isEmpty()){
		            List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
		            for(Map.Entry<String, String> entry : pars.entrySet()) {  
		            	nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
		            }
		            httpost.setEntity(new UrlEncodedFormEntity(nvps,charset));
		        }
		        //----------------put json参数------------------------------------------------------------------------------------------------------------
		        if(json != null && json.trim().length() > 0)
		        {
		            StringEntity reqEntity = new StringEntity(json,charset);
		            httpost.setEntity(reqEntity);
		        }
		        //-----------------设置自定义headers--------------------------------------------------------------------------------------------------------
			 	postHeaders(httpost,headers);
			 	//------------------初始化参数---------------------------------------------------------------------------------------------------------------
		        secureConnectionManager =createManager(url);
		        HttpClient httpclient  = createHttpClient(secureConnectionManager);
		        //------------------执行请求----------------------------------------------------------------------------------------------------------------
		        HttpResponse httpResponse = httpclient.execute(httpost);
		        if(httpResponse.getStatusLine().getStatusCode() == 200){
		        	return  new HttpResult(200, EntityUtils.toString(httpResponse.getEntity()));
		        }else{
		        	return  new HttpResult(httpResponse.getStatusLine().getStatusCode(), EntityUtils.toString(httpResponse.getEntity()));
		        }
		 }catch(Exception se){
			 return  new HttpResult(900, se.getMessage());
		 }finally{
			 if(null!=secureConnectionManager){
				 secureConnectionManager.shutdown();
				 secureConnectionManager.close();
			 }
		 }
	}
	//======================http/https get请求========================================================================================================
	/**
	 * *******************************************************
	 * <p>@Description 发送指定url的get请求</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:08:14</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult get(String url){
		return httpget(url,null,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description 发送带参数的请求</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:08:30</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult get(String url,Map<String,String> pars){
		return httpget(url,pars,null);
	}
	/**
	 * *******************************************************
	 * <p>@Description get请求</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:52:25</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	public HttpResult get(String url,Map<String,String> pars,Map<String,String> headers){
		return httpget(url,pars,headers);
	}
	/**
	 * *******************************************************
	 * <p>@Description get请求</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午8:55:25</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private HttpResult httpget(String url,Map<String,String> pars,Map<String,String> headers){
		 url=durl(url);
		 System.out.println("****************:"+url);
		 HttpGet httpGet = null;
		 PoolingHttpClientConnectionManager secureConnectionManager = null;
		 StringBuffer pab = new StringBuffer();
		 try{
			 	//--------------------在URL后面追加参数------------------------------------------------
			 	if(pars != null && !pars.isEmpty()){
		            if(url.indexOf("?") == -1){
		                url +="?";
		            }
		            if(null!=pars && !pars.isEmpty()){
		            	for (Map.Entry<String, String> entry : pars.entrySet()) {  
		            		pab.append("&"+entry.getKey()+"="+URLEncoder.encode(entry.getValue(),charset));//追加参数
		            	}
		            }
		        }
			 	if(pab.toString().length()>3){
			 		url+=pab.toString().substring(1);
			 	}
			 	//----------------------设置连接参数---------------------------------------------------
			 	httpGet = new HttpGet(url);
			 	httpGet.setConfig(requestConfig);
			 	//-----------------设置自定义headers--------------------------------------------------
			 	getHeaders(httpGet,headers);
			 	//-----------------https支持-------------------------------------------------------
		        secureConnectionManager =createManager(url);
		        HttpClient httpclient  = createHttpClient(secureConnectionManager);
		        //-----------------执行请求---------------------------------------------------------
		        HttpResponse httpResponse = httpclient.execute(httpGet);
		        if(httpResponse.getStatusLine().getStatusCode() == 200){
		        	return  new HttpResult(200, EntityUtils.toString(httpResponse.getEntity()));
		        }else{
		        	return  new HttpResult(httpResponse.getStatusLine().getStatusCode(), EntityUtils.toString(httpResponse.getEntity()));
		        }
		 }catch(Exception se){
			 return  new HttpResult(900, se.getMessage());
		 }finally{
			 if(null!=secureConnectionManager){
				 secureConnectionManager.shutdown();
				 secureConnectionManager.close();
			 }
		 }
	}
	//=============================基础参数配置====================================================================================================
	/**
	 * *******************************************************
	 * <p>@Description </p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月19日 下午1:31:00</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private HttpClient createHttpClient(PoolingHttpClientConnectionManager secureConnectionManager){
		return  HttpClients.custom().setConnectionManager(secureConnectionManager).build();
	}
	/**
	 * *******************************************************
	 * <p>@Description createManager</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年7月20日 下午2:38:09</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private PoolingHttpClientConnectionManager createManager(String url){
		try {
			url=durl(url);
			if(url.toLowerCase().startsWith("https")){
				SSLContext sslContext = SSLContext.getInstance("TLS");
				sslContext.init(null, new TrustManager[] {
					new X509TrustManager() {
						public void checkClientTrusted(X509Certificate ax509certificate[], String s) throws CertificateException {}
						public void checkServerTrusted(X509Certificate ax509certificate[], String s)throws CertificateException {}
						public X509Certificate[] getAcceptedIssuers() {
							return null;
						}
					}
			    }, null);
	            ConnectionSocketFactory plainSocketFactory = new PlainConnectionSocketFactory();
	            SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslContext,new HttpHostnameVerifier());//信任所有证书
				Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
		                    .register("http", plainSocketFactory)
		                    .register("https", ssf)
		                    .build();
				return new PoolingHttpClientConnectionManager(r);
			}
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } 
		return new PoolingHttpClientConnectionManager();
	}
	/**
	 * *******************************************************
	 * <p>@Description post附加请求头</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:38:22</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private void postHeaders(HttpPost httpost,Map<String,String> headers){
		//------------------------公共协议头--------------------------------
		if(null!=header && !header.isEmpty()){
			for (Map.Entry<String, String> entry : header.entrySet()) {  
	 			httpost.addHeader(entry.getKey(),entry.getValue());
	 		}
		}
		//------------------------私有协议头--------------------------------
		if(null!=headers && !headers.isEmpty()){
	 		for (Map.Entry<String, String> entry : headers.entrySet()) {
	 			httpost.addHeader(entry.getKey(),entry.getValue());
	 		}
	 	}
	}
	/**
	 * *******************************************************
	 * <p>@Description get附加头部信息</p>
	 * <p>@Version 1.0.0</p>
	 * <p>@author rebinrebin</p>
	 * <p>@time 2017年9月8日 上午9:39:23</p>
	 * <p>xiefangjian@163.com</p>
	 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
	 * *****************************************************
	 */
	private void getHeaders(HttpGet httpget,Map<String,String> headers){
		//---------------公有协议头-----------------------------------------
		if(null!=header && !header.isEmpty()){
			for (Map.Entry<String, String> entry : header.entrySet()) {  
				httpget.addHeader(entry.getKey(),entry.getValue());
	 		}
		}
		//---------------私有协议头------------------------------------------
		if(null!=headers && !headers.isEmpty()){
	 		for (Map.Entry<String, String> entry : headers.entrySet()) {  
	 			httpget.addHeader(entry.getKey(),entry.getValue());
	 		}
	 	}
	}
	
	//==================================================================================================================================
	public String durl(String url){
		if(null==url){
			return defaulturl;
		}
		if(url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://")){
			return url;
		}else if(null!=defaulturl){
			url=defaulturl+url;
		}
		return url;
	}
	public Map<String, String> getHeader() {
		return header;
	}
	public void setHeader(Map<String, String> header) {
		this.header = header;
	}
	public RequestConfig getRequestConfig() {
		return requestConfig;
	}

	public void setRequestConfig(RequestConfig requestConfig) {
		this.requestConfig = requestConfig;
	}
	
	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}
	public String getDefaulturl() {
		return defaulturl;
	}
	public void setDefaulturl(String defaulturl) {
		this.defaulturl = defaulturl;
	}
}
